﻿using UnityEngine;
using System.Collections;
using BehaviorSystem;
using SkillSystem;

public class SkillExcuteNode : ActionNode
{
    SkillItemData itemData;
    AnimatorComponnent animatorComponnent;
    MoveComponent moveComponent;
    PlayerMoveComponent playerMoveComponent;
    SkillTransmitComponnent skillTransmit;

    public SkillExcuteNode(IBehaviorAgent agent,SkillItemData itemData) : base(agent)
    {
        this.itemData = itemData;
        animatorComponnent = agent.GetComponent<AnimatorComponnent>();
        moveComponent = agent.GetComponent<MoveComponent>();
        playerMoveComponent = agent.GetComponent<PlayerMoveComponent>();
        skillTransmit= agent.GetComponent<SkillTransmitComponnent>();


    }

    public override BTNodeState HandleAction()
    {
        DebugUtils.LogRedInfo("    " + itemData.ActionName);
        if (!string.IsNullOrEmpty( itemData.ActionName))
        {
            animatorComponnent.SetBool(itemData.ActionName, true);
            skillTransmit.transform.LookAt(skillTransmit.TargetObj.transform);
            if (moveComponent)
            {
                moveComponent.IsCanMove = false;
            }
            if (playerMoveComponent)
            {
                playerMoveComponent.IsCanMove = false;
            }
            //skillItemExcutor.TargetPos = skillTransmit.TargetObj.transform.position;


        }

        if (!string.IsNullOrEmpty(itemData.Effect))
        {
            GameObject effectObj = Resources.Load<GameObject>(itemData.Effect);
            if (effectObj != null)
            {
                GameObject go = MonoBehaviour.Instantiate(effectObj, skillTransmit.transform.position, Quaternion.identity);
                SkillEffectDisplayerAgent skillEffectDisplayer = go.GetComponent<SkillEffectDisplayerAgent>();
                if (skillEffectDisplayer != null)
                {
                    skillEffectDisplayer.skillTransmit = skillTransmit;
                }
            }
        }

        CurState = BTNodeState.Success;
        return CurState;
    }

    public override void OnReseted()
    {
        if (!string.IsNullOrEmpty(itemData.ActionName))
        {
            animatorComponnent.SetBool(itemData.ActionName, false);
        }
        if (moveComponent)
        {
            moveComponent.IsCanMove = true;
        }
        if (playerMoveComponent)
        {
            playerMoveComponent.IsCanMove = true;
        }
    }
}
